package com.bardsoftware.foronuvolo.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class AnswerMessageSender extends MessageSender {

	public AnswerMessageSender(
			RootPanel textAreaPanel,
			RootPanel sendButtonPanel, 
			final MessageSendService sendService,
			final String outputElementID, 
			final String errorElementID) {
		super(textAreaPanel, sendButtonPanel, sendService, outputElementID,
				errorElementID);
		sendButtonPanel.clear();
		HorizontalPanel buttonsPanel = new HorizontalPanel();
		sendButtonPanel.add(buttonsPanel );
		
		buttonsPanel.add(getSendButton());
		Button likeButton = new Button("Like");
		buttonsPanel.add(likeButton);
		Button unlikeButton = new Button("Unlike");
		buttonsPanel.add(unlikeButton);		
		class SendLikeHandler implements ClickHandler{
            public void onClick(ClickEvent event) {
                sendButton.setEnabled(false);
                //createNewTagButton.setEnabled(false);
                String textToServer = messageField.getText();
                sendService.sendMessage(
                        "Positive comment: \n"+textToServer,
                        new AsyncCallback<String>() {
                            public void onFailure(Throwable caught) {
                                AnswerMessageSender.this.onFailure(caught);
                            }
                            public void onSuccess(String result) {
                                AnswerMessageSender.this.onSuccess(result);
                            }
                        });
            }
        }
		class SendUnlikeHandler implements ClickHandler{
            public void onClick(ClickEvent event) {
                sendButton.setEnabled(false);
                //createNewTagButton.setEnabled(false);
                String textToServer = messageField.getText();
                sendService.sendMessage(
                        "Negative comment:\n"+textToServer,
                        new AsyncCallback<String>() {
                            public void onFailure(Throwable caught) {
                                AnswerMessageSender.this.onFailure(caught);
                            }
                            public void onSuccess(String result) {
                                AnswerMessageSender.this.onSuccess(result);
                            }
                        });
            }
        }

        // Add a handler to send the name to the server
        likeButton.addClickHandler(new SendLikeHandler());
        unlikeButton.addClickHandler(new SendUnlikeHandler());
	}

}
